import os
import pandas as pd
from my_json import download_json, bulid_download_path
from spider_url import grade_rank_url, province_url

basic_path = str(os.path.dirname(__file__)) + "/"
province = province_url().province
download_path = bulid_download_path(basic_path)


def init():
    columns = ["province_name", "year", "min_grade", "max_rank", "type"]
    url = grade_rank_url()
    map = download_json(url.map_url, download_path + "map.json")["data"]["data"]
    d = []
    for k_province, v_province in map.items():
        province_name = province[k_province]
        for k_year, v_year in v_province.items():
            level = v_year["level"]["0"]["id"]
            for k_id, v_id in v_year.items():
                if k_id == "level":
                    continue
                id = v_id["id"]
                type = v_id["name"]
                rank = download_json(url.to_string(k_year, k_province, id, level),
                                     download_path + k_year + "_" + province_name + "_" + type + "_" + "rank.json")[
                    "data"]["list"]
                for v_rank in rank:
                    min_grade = v_rank["rank"]
                    max_rank = v_rank["total"]
                    t = [province_name, k_year, min_grade, max_rank, type]
                    d.append(t)
    d = pd.DataFrame(d, columns=columns)
    d.to_csv(basic_path + "grade_rank.csv", encoding="utf-8-sig")


init()
